package com.foton.lotterydomain.activity.service.deploy.impl;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.foton.lotterydomain.activity.model.req.ActivityConfigReq;
import com.foton.lotterydomain.activity.repository.IActivityRepository;
import com.foton.lotterydomain.activity.service.deploy.IActivityDeploy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author RenPu
 * @Date 2023/12/1 16:47
 * @Version 1.0
 * @Description: 抽奖活动-发布实现层
 **/
@Slf4j
@Service
public class ActivityDeployImpl implements IActivityDeploy {

    @Resource
    private IActivityRepository activityRepository;

    /**
     * 创建活动
     *
     * @param req
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public void createActivity(ActivityConfigReq req) {
        try {
            //抽奖活动，抽奖策略，抽奖策略明细，抽奖商品的关系
            //抽奖活动-抽奖策略-抽奖策略明细 ,都是一对一的关系，抽奖活动-抽奖商品属于 一对多的关系
            log.info("创建活动配置开始，activityId：{}", req.getActivityId());
            //添加抽奖活动
            activityRepository.addActivity(req.getActivityConfigRich().getActivity());

            //添加抽奖策略
            activityRepository.addStrategy(req.getActivityConfigRich().getStrategy());

            //添加抽奖策略明细
            activityRepository.addStrategyDetailList(req.getActivityConfigRich().getStrategyDetailVOS());

            //添加抽奖奖品
            activityRepository.addAward(req.getActivityConfigRich().getAwardList());
        } catch (DuplicateKeyException e) {
            log.error("创建活动配置失败，唯一索引冲突 activityId：{} reqJson：{}", req.getActivityId(), JSONUtil.toJsonStr(req), e);
        }
    }


    /**
     * 修改活动
     *
     * @param req
     */
    @Override
    public void updateActivity(ActivityConfigReq req) {

    }
}
